package com.zzty.springmybatis.common;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBUtil {
    private static String driver;
    private static String url;
    private static String userName;
    private static String pwd;
    private static Connection cn;
    private static PreparedStatement pst;
    static {
        try {
            
            	driver=PropertyUtil.prop.getProperty("driver");
            	url=PropertyUtil.prop.getProperty("url");
            	userName=PropertyUtil.prop.getProperty("username");
            	pwd=PropertyUtil.prop.getProperty("password");
            	Class.forName(driver);
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            
        
    }

    public static void closeConnection() throws Exception {
        if (pst != null) {
        	pst.close();
        	pst=null;
        }

        if (cn != null) {
            cn.close();
            cn=null;
        }
    }
    
    
    public static PreparedStatement getPreparedStatement(String sql){
    	 if (cn == null) {
             try {
                 cn = DriverManager.getConnection(url, userName, pwd);
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
    	 try {
			pst=cn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	return pst;
    	 
    	 
    }

}
